package com.bw.shoppingmall.mvvm.viewmodel;

import androidx.databinding.ObservableField;


import com.bw.mvvm_comom.entity.BaseEntity;
import com.bw.mvvm_comom.filed.UIFiled;
import com.bw.mvvm_comom.mvvm.BaseViewModel;
import com.bw.mvvm_comom.utils.SpUtils;
import com.bw.shoppingmall.MainActivity;
import com.bw.shoppingmall.mvvm.model.SplashModel;
import com.bw.shoppingmall.mvvm.model.entity.TokenEntity;
import com.bw.shoppingmall.timmer.TimeHandler;
import com.bw.shoppingmall.timmer.TimeThread;

import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class SplashViewModel extends BaseViewModel<SplashModel> {
    private TimeHandler handler;
    public ObservableField<String> timeObserable = new ObservableField<>("3S");
    @Override
    public void onCreate() {
        super.onCreate();
        requestToken();
        handler = new TimeHandler(this);
    }
    private void requestToken(){
        HashMap<String,Object> map = new HashMap<>();
        map.put("imie",""+System.currentTimeMillis());
        m.requestToken(map)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(this);
    }

    @Override
    protected void result(BaseEntity baseEntity) {
        if (baseEntity.getMessage().equals("imie为空")){
            showMsg(baseEntity.getMessage());
            return;
        }
        if (baseEntity.getMessage().equals("sign为空")){
            showMsg(baseEntity.getMessage());
            return;
        }
        TokenEntity tokenEntity = (TokenEntity) baseEntity;
        SpUtils.writeToken(tokenEntity.getValues().token);
        new TimeThread(handler).start();

    }
    public void SkipTimmer(){
        HashMap<String,Object> map = new HashMap<>();
        map.put(UIFiled.PAGECLAZZ, MainActivity.class);
        startPage(map);
    }
}
